package br.com.lazyprogrammer.worldsoccermanager;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ToggleButton;

import com.facebook.Session;
import com.facebook.SessionState;

import br.com.lazyprogrammer.worldsoccermanager.fragments.main.LoginFragment;
import br.com.lazyprogrammer.worldsoccermanager.fragments.main.RegisterFragment;
import br.com.lazyprogrammer.worldsoccermanager.preferences.Preferences;
import br.com.lazyprogrammer.worldsoccermanager.util.Constants;
import br.com.lazyprogrammer.worldsoccermanager.util.PoolData;


public class MainActivity extends Activity implements View.OnClickListener, Session.StatusCallback {

    private Button buttonConnectFb, buttonConnectGp;
    private ToggleButton toggleLogin;
    private ToggleButton toggleRegister;

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {

        }
    }

    public void connectFacebook() {
        Session sesion = Session.openActiveSession(this, true, this);
    }

    public void connectGooglePlus() {

    }

    public void goToConfirmAccountScreen() {
        startActivityForResult(new Intent(this, ConfirmAccountActivity.class), Constants.RESULT_VALIDATE);
    }

    public void goToForgotPasswordScreen() {
        startActivity(new Intent(this, ForgotPasswordActivity.class));
    }

    public void goToHomeScreen() {
        startActivity(new Intent(getApplicationContext(), HomeActivity.class));
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.RESULT_VALIDATE:
                    goToHomeScreen();
                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {
        toggleLogin.setChecked(false);
        toggleRegister.setChecked(false);
        if (getFragmentManager().findFragmentById(R.id.layout_main) != null)
            getFragmentManager()
                    .beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_NONE)
                    .remove(getFragmentManager()
                            .findFragmentById(R.id.layout_main))
                    .commit();
        else
            finish();
    }

    @Override
    public void onClick(View view) {
        if (!view.equals(buttonConnectFb) && !view.equals(buttonConnectGp)) {
            toggleLogin.setChecked(view.equals(toggleLogin));
            toggleRegister.setChecked(view.equals(toggleRegister));
        }
        if (view.equals(toggleLogin))
            openFragment(new LoginFragment());
        else if (view.equals(toggleRegister))
            openFragment(new RegisterFragment());
        else if (view.equals(buttonConnectFb))
            connectFacebook();
        else if (view.equals(buttonConnectGp))
            connectGooglePlus();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(R.layout.activity_main);

        PoolData.mPref = new Preferences(this, getString(R.string.app_name));

        buttonConnectFb = (Button) findViewById(R.id.button_connect_fb);
        buttonConnectGp = (Button) findViewById(R.id.button_connect_gp);

        toggleLogin = (ToggleButton) findViewById(R.id.toggle_goto_login);
        toggleRegister = (ToggleButton) findViewById(R.id.toggle_goto_register);

        buttonConnectFb.setOnClickListener(this);
        buttonConnectGp.setOnClickListener(this);
        toggleLogin.setOnClickListener(this);
        toggleRegister.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void openFragment(Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        Fragment currentFragment = fm.findFragmentById(R.id.layout_main);
        FragmentTransaction ft;
        if (currentFragment != null) {
            if (currentFragment.getClass().equals(fragment.getClass()))
                return;

            ft = fm.beginTransaction();
            ft.setTransition(FragmentTransaction.TRANSIT_NONE);
            ft.remove(currentFragment);
            ft.commit();
        }
        ft = fm.beginTransaction();
        ft.setCustomAnimations(R.anim.anim_enter, R.anim.anim_exit, R.anim.anim_pop_enter, R.anim.anim_pop_exit);
        ft.replace(R.id.layout_main, fragment);
        ft.commit();
    }
}
